home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / genctxt.zip / CHAP4.TXT < prev    next >
Text File  |  1987-11-21  |  32KB  |  718 lines

  1.  
  2.                  Chapter 4 - Assignment & Logical compares
  3.  
  4.  
  5.              Throughout  this  chapter,  references  are  given   to
  6.         various  ranges  of  variables.  Your  compiler  may  use  a
  7.         different range for some of the variables since the proposed
  8.         ANSI  standard does not define specific limits for all  data
  9.         types.  Consult the documentation for your compiler for  the
  10.         exact range for each of the variable types.
  11.  
  12.                        INTEGER ASSIGNMENT STATEMENTS
  13.  
  14.              Load the file INTASIGN.C and display it for an  example
  15.         of  assignment statements.   Three variables are defined for
  16.         use  in the program and the rest of the program is merely  a
  17.         series of illustrations of various assignments.   The  first
  18.         two  lines  of  the assignment statements  assign  numerical
  19.         values  to "a" and "b",  and the next five lines  illustrate
  20.         the  five  basic arithmetic functions and how to  use  them.
  21.         The  fifth is the modulo operator and gives the remainder if
  22.         the two variables were divided.   It can only be applied  to
  23.         "int"  or  "char"  type  variables,   and  of  course  "int"
  24.         extensions such as "long",  "short",  etc.  Following these,
  25.         there  are two lines illustrating how to combine some of the
  26.         variables  in  some complex math expressions.   All  of  the
  27.         above examples should require no comment except to say  that
  28.         none  of the equations are meant to be  particularly  useful
  29.         except  as illustrations.  The "char" type variable will  be
  30.         defined in the description of the next example program.
  31.  
  32.              The  expressions  in  lines 17  and  18  are  perfectly
  33.         acceptable  as given, but we will see later in this  chapter
  34.         that  there is another way to write these for  more  compact
  35.         code.
  36.  
  37.              This leaves us with the last two lines which may appear
  38.         to  you  as being very strange.   The C compiler  scans  the
  39.         assignment  statement from right to left,  (which may seem a
  40.         bit odd since we do not read that way),  resulting in a very
  41.         useful construct,  namely the one given here.   The compiler
  42.         finds the value 20, assigns it to "c", then continues to the
  43.         left finding that the latest result of a calculation  should
  44.         be  assigned to "b".   Thinking that the latest  calculation
  45.         resulted in a 20,  it assigns it to "b" also,  and continues
  46.         the leftward scan assigning the value 20 to "a" also.   This
  47.         is a very useful construct when you are initializing a group
  48.         of  variables.   The last statement illustrates that  it  is
  49.         possible  to actually do some calculations to arrive at  the
  50.         value  which  will be assigned to all three  variables.   In
  51.         fact,  the rightmost expression can contain variables,  even
  52.         "a", "b", & "c".
  53.  
  54.              The  program has no output, so compiling and  executing
  55.         this  program  will be very uninteresting.  Since  you  have
  56.  
  57.  
  58.                                   Page 18
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                  Chapter 4 - Assignment & Logical compares
  69.  
  70.  
  71.         already  learned how to display some integer  results  using
  72.         the "printf" function, it would be to your advantage to  add
  73.         some output statements to this program to see if the various
  74.         statements do what you think they should do.
  75.  
  76.              This would be a good time for a preliminary  definition
  77.         of  a  rule to be followed in C.   The data definitions  are
  78.         always given before any executable statements in any program
  79.         block.   This is why the variables are defined first in this
  80.         program and in every C program.  If you try to define a  new
  81.         variable after executing some statements, your compiler will
  82.         issue an error.
  83.  
  84.                            ADDITIONAL DATA TYPES
  85.  
  86.              Loading and editing MORTYPES.C will illustrate how some
  87.         additional  data  types can be used.   Once  again  we  have
  88.         defined  a  few integer type variables which you  should  be
  89.         fairly  familiar  with  by now,  but we have added  two  new
  90.         types, the "char", and the "float".
  91.  
  92.              The  "char"  type  of data is nearly the  same  as  the
  93.         integer except that it can only be assigned numerical values
  94.         between  -128 to 127 on most implementations of C, since  it
  95.         is  stored in only one byte of memory.  The "char"  type  of
  96.         data is usually used for ASCII data, more commonly known  as
  97.         text.  The text you are reading was originally written on  a
  98.         computer with a word processor that stored the words in  the
  99.         computer  one character per byte.  In contrast, the  integer
  100.         data  type  is  stored in two bytes of  computer  memory  on
  101.         nearly all microcomputers.
  102.  
  103.                               DATA TYPE MIXING
  104.  
  105.              It  would be profitable at this time to discuss the way
  106.         C handles the two types "char" and "int".  Most functions in
  107.         C  that are designed to operate with integer type  variables
  108.         will work equally well with character type variables because
  109.         they  are a form of an integer variable.   Those  functions,
  110.         when called on to use a "char" type variable,  will actually
  111.         promote  the "char" data into integer data before using  it.
  112.         For this reason, it is possible to mix "char" and "int" type
  113.         variables in nearly any way you desire.   The compiler  will
  114.         not get confused,  but you might.  It is good not to rely on
  115.         this too much, but to carefully use only the proper types of
  116.         data where they should be used.
  117.  
  118.               The second new data type is the "float" type of  data,
  119.         commonly  called floating point data.  This is a  data  type
  120.         which  usually  has a very large range, a  large  number  of
  121.         significant digits, and a large number of computer words are
  122.  
  123.  
  124.                                   Page 19
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                  Chapter 4 - Assignment & Logical compares
  135.  
  136.  
  137.         required  to store it.  The "float" data type has a  decimal
  138.         point associated with it and has an allowable range of  from
  139.         3.4E-38   to  3.4E+38  when  using  most  C   compilers   on
  140.         microcomputers,  and  is  composed of  about  7  significant
  141.         digits.
  142.  
  143.                        HOW TO USE THE NEW DATA TYPES
  144.  
  145.              The  first three lines of the program assign values  to
  146.         all nine of the defined variables so we can manipulate  some
  147.         of the data between the different types.
  148.  
  149.              Since,  as  mentioned above,  a "char" data type is  in
  150.         reality  an "integer" data type,  no special  considerations
  151.         need be taken to promote a "char" to an "int",  and a "char"
  152.         type data field can be assigned to an "int" variable.   When
  153.         going the other way, there is no standard, so you may simply
  154.         get garbage if the value of the integer variable is  outside
  155.         the  range of the "char" type variable.   It will  translate
  156.         correctly if the value is within the range of -128 to 127.
  157.  
  158.              The   third   line   illustrates  the   simplicity   of
  159.         translating an integer into a "float",  simply assign it the
  160.         new  value  and the system will do  the  proper  conversion.
  161.         When  going  the  other  way  however,  there  is  an  added
  162.         complication.   Since  there may be a fractional part of the
  163.         floating  point number,  the system must decide what  to  do
  164.         with it.  By definitions , it will truncate it.
  165.  
  166.              This program produces no output, and we haven't covered
  167.         a way to print out "char" and "float" type variables, so you
  168.         can't  really  get  in  to this program and  play  with  the
  169.         results, but the next program will cover this for you.
  170.  
  171.